package com.yohann.exam_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yohann.exam_service.entity.Writing;
import com.yohann.exam_service.mapper.WritingMapper;
import com.yohann.exam_service.service.WritingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yohann.exam_service.utils.YoudaoUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 写译表 服务实现类
 * </p>
 *
 * @author Yohann
 * @since 2020-11-19
 */
@Service
public class WritingServiceImpl extends ServiceImpl<WritingMapper, Writing> implements WritingService {
    @Override
    public Map<String, Object> queryWriting(int current, int limit, Integer type) {
        Page<Writing> page = new Page<>(current, limit);

        LambdaQueryWrapper<Writing> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Writing::getId, Writing::getTitle, Writing::getCreateTime)
                .eq(Writing::getType, type)
                .orderByDesc(Writing::getCreateTime);

        this.page(page, wrapper);

        //将查询到的总条数、结果集存入map中并返回
        Map<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("rows", page.getRecords());

        return map;
    }

    /**
     * 查询写译题目
     *
     * @param type 题目类型
     * @return 题目集合
     */
    @Override
    public List<Writing> queryWriting(int type) {
        LambdaQueryWrapper<Writing> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Writing::getId, Writing::getTitle)
                .eq(Writing::getType, type)
                .orderByDesc(Writing::getCreateTime);

        return list(wrapper);
    }

    @Override
    public String translate(String str) {
        return YoudaoUtils.translate(str);
    }

    @Override
    public Map<String, Object> correct(String str) {
        return YoudaoUtils.correct(str);
    }
}
